/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.app.browser.history.HistoryStatus;
import fever.app.browser.history.strategy.AbstractHistoryStrategy;
import fever.log.FeverDebug;

import flash.external.ExternalInterface;

/**
 * Browser history managment using <strong>SWFAddress</strong> 
 * Javascript engine.
 * 
 * <p>More info : http://www.asual.com/swfaddress/
 * 
 * @author Romain Ecarnot
 */
class fever.app.browser.history.strategy.SWFAddressStrategy extends AbstractHistoryStrategy
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _instance : SWFAddressStrategy;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/**
	 * Javascript <strong>SWFAddress</strong> method name for 
	 * {@code getId()} method.
	 */
	public static var getIdMETHOD : String = 'getId';	
	/**
	 * Javascript <strong>SWFAddress</strong> method name for 
	 * {@code getValue()} method.
	 */
	public static var getValueMETHOD : String = 'getValue';	
	/**
	 * Javascript <strong>SWFAddress</strong> method name for 
	 * {@code setValue( s )} method.
	 */
	public static var setValueMETHOD : String = 'setValue';	
	/**
	 * Javascript <strong>SWFAddress</strong> method name for 
	 * {@code getSWFAddressValue()} method.
	 */
	public static var getSWFAddressValueMETHOD : String = 'getSWFAddressValue';	
	/**
	 * Javascript <strong>SWFAddress</strong> method name for 
	 * {@code setSWFAddressValue( s )} method.
	 */
	public static var setSWFAddressValueMETHOD : String = 'setSWFAddressValue';
	
    
    //-------------------------------------------------------------------------
    // Public API
    //-------------------------------------------------------------------------
	
	/**
	 * Returns {@link SWFAddressStrategy} unique instance.
	 * 
	 * <p>Singleton pattern.
	 */
	public static function getInstance(  ) : SWFAddressStrategy
	{
		if( ! _instance ) _instance = new SWFAddressStrategy();
		return _instance;
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function SWFAddressStrategy()
	{
    	super();
    }
    
    private function _init(  ) : Void 
    {
    	 if ( ExternalInterface.available )
        {
			ExternalInterface.addCallback(
				getSWFAddressValueMETHOD, 
				this, 
			    function() : String {return this._sLink; }
			);
			
			ExternalInterface.addCallback( 
				setSWFAddressValueMETHOD, 
				this, 
			    register
			);
			
			_sLink = '';
        }
        else
        {
        	FeverDebug.ERROR( 'ExternalInterface is not available here : ' + this );	
        }
    }
    
    private function _register() : Void 
	{
		ExternalInterface.call( 'SWFAddress.' + setValueMETHOD, _sLink );
	}
	
    private function _getState() : HistoryStatus
	{
        var state : String = String( ExternalInterface.call( 'SWFAddress.' + getValueMETHOD ) );
        var id : String = String( ExternalInterface.call( 'SWFAddress.' + getIdMETHOD ) );
		
        if ( !id ) state = _sLink;
        
        return new HistoryStatus( state, null );
	}
}